unit uSAF0018_02;

interface

uses
   Windows,
   Messages,
   SysUtils,
   Variants,
   Classes,
   Graphics,
   Controls,
   Forms,
   Dialogs,
   ComCtrls,
   ToolWin,
   Menus,
   ActnList,
   ImgList,
   ExtCtrls,
   DB,
   ADODB,
   Grids,
   DBGrids,
   DBCtrls,
   StdCtrls,
   WideStrings,
   FMTBcd;

type
   TfrmPrincipal = class(TForm)
      pmExportar: TPopupMenu;
      miExportarXLS: TMenuItem;
      miExportarCSV: TMenuItem;
      miExportarTXT: TMenuItem;
      actExportar: TActionList;
      actExportarXLS: TAction;
      actExportarCSV: TAction;
      actExportarTXT: TAction;
      actPrincipal: TActionList;
      actInserir: TAction;
      actEditar: TAction;
      actConsultar: TAction;
      actFechar: TAction;
      ilPrincipal: TImageList;
      pnlSuperior: TPanel;
      pnlCentro: TPanel;
      imgLogo: TImage;
      dbgrdLista: TDBGrid;
      actDeletar: TAction;
      lblTIPO: TLabel;
      dsLista: TDataSource;
      pnlInferior: TPanel;
      statPrincipal: TStatusBar;
      tlbPrincipal: TToolBar;
      btnInserir: TToolButton;
      btnEditar: TToolButton;
      btnDeletar: TToolButton;
      btnConsultar: TToolButton;
      btnS1: TToolButton;
      btnExportar: TToolButton;
      btnS2: TToolButton;
      btnFechar: TToolButton;
      btnS3: TToolButton;
      dbnvgrLista: TDBNavigator;

      procedure FormShow(Sender: TObject);
      procedure actFecharExecute(Sender: TObject);
      procedure FormKeyPress(Sender: TObject; var Key: Char);
      procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
      procedure actInserirExecute(Sender: TObject);
      procedure actEditarExecute(Sender: TObject);
      procedure actConsultarExecute(Sender: TObject);
      procedure dsListaStateChange(Sender: TObject);
      procedure actDeletarExecute(Sender: TObject);
      procedure actExportarXLSExecute(Sender: TObject);
      procedure actExportarCSVExecute(Sender: TObject);
      procedure dsListaDataChange(Sender: TObject; Field: TField);

   private

      { Private declarations }

   public

      { Public declarations }

   end;

var
   frmPrincipal: TfrmPrincipal;

implementation

{$R *.dfm}

uses
   uLibGlobal,
   uSAF0018_01,
   uSAF0018_03;

procedure TfrmPrincipal.actConsultarExecute(Sender: TObject);
begin
   try
      FreeAndNil(frmInsUpd);
   finally
      frmInsUpd := TfrmInsUpd.Create(nil);
      frmInsUpd._INSUPD_TIPO := 2;

      frmInsUpd._INSUPD_ID :=
         DMDados.qryLista.FieldByName('ID_EMPRESA').AsInteger;
      frmInsUpd._INSUPD_ID2 :=
         DMDados.qryLista.FieldByName('ID_SITE').AsInteger;

      frmInsUpd.ShowModal;
   end;
end;

procedure TfrmPrincipal.actDeletarExecute(Sender: TObject);
var
   chaveID: integer;
begin
   if not (DMDados.tblTabela.State in [dsInsert, dsEdit]) then
   begin
      if Application.MessageBox(
         PChar('DESEJA REALMENTE EXCLUIR O REGISTRO ?'),
         PChar(Application.Title),
         MB_YESNO + MB_ICONQUESTION) = IDYES then
      begin
         chaveID := DMDados.qryLista.FieldByName('ID_SITE').AsInteger;

         fAtualizaDS(DMDados.tblTabela);

         DMDados.tblTabela.Locate(
            'ID_SITE',
            chaveID, [loCaseInsensitive, loPartialKey]);

         DMDados.tblTabela.Delete;

         fAtualizaDS(DMDados.qryLista);
      end;
   end;
end;

procedure TfrmPrincipal.actEditarExecute(Sender: TObject);
begin
   try
      FreeAndNil(frmInsUpd);
   finally
      frmInsUpd := TfrmInsUpd.Create(nil);

      frmInsUpd._INSUPD_TIPO := 1;
      frmInsUpd._INSUPD_ID :=
         DMDados.qryLista.FieldByName('ID_EMPRESA').AsInteger;
      frmInsUpd._INSUPD_ID2 :=
         DMDados.qryLista.FieldByName('ID_SITE').AsInteger;

      frmInsUpd.ShowModal;
   end;
end;

procedure TfrmPrincipal.actExportarCSVExecute(Sender: TObject);
begin
   fExportaCSV(DMDados.dsLista,
      ExtractFilePath(ParamStr(0)) + 'CSV\' + _sistemaNome + ' - ' + _sistemaDescricao);
end;

procedure TfrmPrincipal.actExportarXLSExecute(Sender: TObject);
begin
   fExportaXLS(DMDados.dsLista,
      ExtractFilePath(ParamStr(0)) + 'XLS\' + _sistemaNome + ' - ' + _sistemaDescricao);
end;

procedure TfrmPrincipal.actFecharExecute(Sender: TObject);
begin
   Self.Close;
end;

procedure TfrmPrincipal.actInserirExecute(Sender: TObject);
begin
   try
      FreeAndNil(frmInsUpd);
   finally
      frmInsUpd := TfrmInsUpd.Create(nil);
      frmInsUpd._INSUPD_TIPO := 0;
      frmInsUpd.ShowModal;
   end;
end;

procedure TfrmPrincipal.dsListaDataChange(Sender: TObject; Field: TField);
begin
   statPrincipal.Panels[1].Text :=
      'Registro atual: ' +
      FormatFloat('#,###,##0', dsLista.DataSet.RecNo) +
      ' / ' +
      'Total de Registros: ' +
      FormatFloat('#,###,##0', dsLista.DataSet.RecordCount);

   Application.ProcessMessages;
   statPrincipal.Update;
end;

procedure TfrmPrincipal.dsListaStateChange(Sender: TObject);
begin
   actInserir.Enabled := (dsLista.State in [dsBrowse]);

   actEditar.Enabled := (dsLista.State in [dsBrowse])
      and (dsLista.DataSet.RecordCount > 0);

   actDeletar.Enabled := (dsLista.State in [dsBrowse])
      and (dsLista.DataSet.RecordCount > 0);

   actConsultar.Enabled := (dsLista.State in [dsBrowse])
      and (dsLista.DataSet.RecordCount > 0);

   actExportarXLS.Enabled := (dsLista.State in [dsBrowse])
      and (dsLista.DataSet.RecordCount > 0);

   actExportarCSV.Enabled := (dsLista.State in [dsBrowse])
      and (dsLista.DataSet.RecordCount > 0);

   actExportarTXT.Enabled := (dsLista.State in [dsBrowse])
      and (dsLista.DataSet.RecordCount > 0);
end;

procedure TfrmPrincipal.FormKeyDown(Sender: TObject; var Key: Word;
   Shift: TShiftState);
begin
   fNotCtrlDelete(Key, Shift);
end;

procedure TfrmPrincipal.FormKeyPress(Sender: TObject; var Key: Char);
begin
   fTabToEnter(Key, Self);
end;

procedure TfrmPrincipal.FormShow(Sender: TObject);
begin
   ShortDateFormat := 'dd/mm/yyyy';
   ShortTimeFormat := 'hh:nn:ss';

   if not fValidaWOS(ParamStr(1)) then
      Application.Terminate;

   fMontarTelaPrincipal(Self, 400, 660);
   fCarregarLogo(imgLogo, _sistema);
   Self.Caption := _sistemaNome + ' - ' + _sistemaDescricao;

   lblTIPO.Caption :=
      FormatFloat('000000', DMDados._TIPO_CHAVE) + ' - ' +
      StringReplace(DMDados._TIPO_DSC, '_', ' ', [rfReplaceAll]);

   {$REGION 'CADASTRA TELEFONE PARA EMPRESA'}
   if (DMDados._TIPO = 'SAF0018') then
   begin
      dsLista.DataSet := DMDados.qryLista;
   end;
   {$ENDREGION}
end;

end.

